﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

namespace Bussiness
{
    /// <summary>
    /// 工况数据类
    /// </summary>
    public class ThermalTestSampleCondition
    {
        /// <summary>
        /// 试验报告标识号
        /// </summary>
        public int ReportID { get; set; }
        /// <summary>
        /// 实时时间
        /// </summary>
        public DateTime SampleTime { get; set; }
        /// <summary>
        /// 采况值
        /// </summary>
        public string SampleValue { get; set; }
        /// <summary>
        /// 热力试验有效性
        /// </summary>
        public int State { get; set; }
        /// <summary>
        /// 删除实验实时数据
        /// </summary>
        /// <param name="reportid"></param>
        public void DeleteSample(int reportid)
        {
            string sql = "DELETE FROM ADMINISTRATOR.ThermalTestSampleCondition WHERE ReportId=" + reportid;
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            link.Excute(sql);
        }

        public string ReadParament()
        {
            string paraCollection="";
            string sql = "SELECT VarChar(SampleValue) FROM ADMINISTRATOR.ThermalTestSampleCondition WHERE REPORTID=" + ReportID + " AND STATE=1";
            try
            {
                DB2Provider.DataLink link = new DB2Provider.DataLink();
                DataTable dt = link.ExcuteRetureTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                    paraCollection = dt.Rows[0][0].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return paraCollection;

        }
        /// <summary>
        /// 新增
        /// </summary>
        public void Insert()
        {
            string sql = "INSERT INTO ADMINISTRATOR.ThermalTestSampleCondition(ReportID,SampleTime,SampleValue,State) VALUES(" + ReportID + ",'" + SampleTime + "','" + SampleValue + "'," + State + ")";
            DB2Provider.DataLink link = new DB2Provider.DataLink();
            link.Excute(sql);
        }

        public DataTable ReadParamentValue()
        {
            DataTable returnDT = GenerColumn(ReadParament());

            string sql = "SELECT * FROM ADMINISTRATOR.ThermalTestSampleCondition WHERE REPORTID=" + ReportID + " AND STATE=1";
            try
            {
                DB2Provider.DataLink link = new DB2Provider.DataLink();
                DataTable dt = link.ExcuteRetureTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = returnDT.NewRow();
                        string paraCollection=dt.Rows[i][2].ToString();
                        string[] para = paraCollection.Split(';');
                        string[] paraNameValue;
                        string paraValue = "";
                        for (int j = 0; j < para.Length; j++)
                        {
                            
                            paraNameValue = para[j].Split(',');
                            paraValue = paraNameValue[1];
                            dr[j+1] = paraValue.ToString();
                        }

                        dr["时间"] = dt.Rows[i][1].ToString();
                        dr["有效性"] = dt.Rows[i][3].ToString();
                        returnDT.Rows.Add(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returnDT;

        }

        public DataTable ReadParamentValue(int templateId)
        {
            DataTable returnDT = GenerColumn(ReadParament(),templateId);
            Bussiness.ThermalTestTemplate TestTemplate = new Bussiness.ThermalTestTemplate();
            string sql = "SELECT * FROM ADMINISTRATOR.ThermalTestSampleCondition WHERE REPORTID=" + ReportID + " AND STATE=1";
            try
            {
                DB2Provider.DataLink link = new DB2Provider.DataLink();
                DataTable dt = link.ExcuteRetureTable(sql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = returnDT.NewRow();
                        string paraCollection = dt.Rows[i][2].ToString();
                        string[] para = paraCollection.Split(';');
                        string[] paraNameValue;
                        string paraValue = "";
                        int colIndex = 0;
                        for (int j = 0; j < para.Length; j++)
                        {
                            
                            paraNameValue = para[j].Split(',');
                            bool exist = TestTemplate.IsExitTemplatePara(templateId, paraNameValue[0]);
                            if (exist)
                            {
                                colIndex += 1;
                                paraValue = paraNameValue[1];
                                dr[colIndex] = paraValue.ToString();
                            }
                        }

                        dr["时间"] = dt.Rows[i][1].ToString();
                        dr["有效性"] = dt.Rows[i][3].ToString();
                        returnDT.Rows.Add(dr);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returnDT;

        }

        public DataTable GenerColumn(string paraCollection, int templateId)
        {
            DataTable dt = new DataTable();
            DataColumn colTime = new DataColumn("时间");
            dt.Columns.Add(colTime);

            string[] para = paraCollection.Split(';');
            string[] paraNameValue;
            string paraName = "";
            ThermaltestPara Para = new ThermaltestPara();
            Bussiness.ThermalTestTemplate TestTemplate = new Bussiness.ThermalTestTemplate();

            for (int i = 0; i < para.Length; i++)
            {
                try
                {
                    DataColumn col = new DataColumn();
                    paraNameValue = para[i].Split(',');
                    bool exist = TestTemplate.IsExitTemplatePara(templateId, paraNameValue[0]);
                    if (exist)
                    {
                        paraName = Para.GetParaDesc(paraNameValue[0]);

                        col.ColumnName = paraName;
                        dt.Columns.Add(col);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            DataColumn colState = new DataColumn("有效性");
            dt.Columns.Add(colState);
            return dt;
        }


        public DataTable GenerColumn(string paraCollection)
        {
            DataTable dt = new DataTable();
            DataColumn colTime = new DataColumn("时间");
            dt.Columns.Add(colTime);

            string[] para = paraCollection.Split(';');
            string[] paraNameValue;
            string paraName = "";
            ThermaltestPara Para = new ThermaltestPara();
            for (int i = 0; i < para.Length; i++)
            {
                try
                {
                    DataColumn col = new DataColumn();
                    paraNameValue = para[i].Split(',');

                    paraName = Para.GetParaDesc(paraNameValue[0]);

                    col.ColumnName = paraName;
                    dt.Columns.Add(col);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            
            DataColumn colState = new DataColumn("有效性");
            dt.Columns.Add(colState);
            return dt;
        }
    }

    public class DisplayValue
    {
        public string ReportTime { get; set; }
  
        

    }
}
